﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;


namespace BookClubIt
{
    public partial class meetings : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {

            DTO.Meetings meetings = BookClubProcessorInstance.GetMeetings(CID);

            rptMeetingList.DataSource = meetings;
            rptMeetingList.DataBind();
            if (meetings.Count == 0)
            {
                pnlNoMeetings.Visible = true;
                rptMeetingList.Visible = false;
            }
            if (BookClubProcessorInstance.IsLeader(CurrentUserId, CID))
            {

                btnOpenModal.Visible = true;
            }


        }
        protected void rptMeetingList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                DTO.Meeting meeting = e.Item.DataItem as DTO.Meeting;
                HyperLink lnkMeetingLink = (HyperLink)item.FindControl("lnkMeetingLink");
                Label lblMeetingCreated = (Label)item.FindControl("lblMeetingCreated");

                lnkMeetingLink.NavigateUrl = "meetingdetail.aspx?id=" + CID + "&mid=" + meeting.Id;
                lnkMeetingLink.Text = meeting.Title;
              
            }

        }

        protected void btnSaveMeeting_Click(Object sender, EventArgs e)
        {
            if (BookClubProcessorInstance.IsBookClubMember(CurrentUserId,CID))
            {
                DTO.Meeting meeting = new DTO.Meeting
                {
                    Book_Club_Id = CID,
                    User_Id = CurrentUserId,
                    Title = txtMeetingTitle.Text.ToString(),
                    Street = txtStreet.Text.ToString(),
                    City = txtCity.Text.ToString(),
                    State = ddlStateList.SelectedValue.ToString(),
                    Zipcode = txtZipcode.Text.ToString(),
                    Created = DateTime.Now
                };

                BookClubProcessorInstance.SaveMeeting(meeting);
                BookClubProcessorInstance.AddClubActivity(CID, CurrentUserId, "ADDMEETING", meeting.Id);
            }
        }
    }
}